package com.chzc.fm.duotin.adapter.recyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.bean.channel.ChannelType;

import java.util.List;

/**
 * Created by Administrator on 2016/6/24.
 */
public class ChannelGridAdapter extends RecyclerView.Adapter<ChannelGridAdapter.ChannelGridHolder> {

    private LayoutInflater inflater;

    private mItemClickListener itemClickListener;

    private List<ChannelType> typeList;

    private Context context;

    public ChannelGridAdapter(Context context, List<ChannelType> typeList ) {

        this.context = context;
        this.typeList = typeList;

        inflater = LayoutInflater.from(context);
    }

    /**
     * 设置监听事件
     * @param listener
     */
    public void setOnItemClickListener(mItemClickListener listener)
    {
        this.itemClickListener = listener;
    }


    @Override
    public ChannelGridHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View Itemview = inflater.inflate(R.layout.adapter_channel_grid,null);

        return new ChannelGridHolder(Itemview);
    }

    @Override
    public void onBindViewHolder(ChannelGridHolder holder, final int position) {

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClickListener.onItemClick(position);
            }
        });

        holder.ivicon.setImageResource(typeList.get(position).getImageid());
        holder.tvtitle.setText(typeList.get(position).getTitle());

    }

    @Override
    public int getItemCount() {

        return typeList == null ? 0 : typeList.size();
    }


    public class ChannelGridHolder extends RecyclerView.ViewHolder {
        ImageView ivicon;
        TextView tvtitle;

        public ChannelGridHolder(View itemView) {
            super(itemView);

            ivicon = (ImageView) itemView.findViewById(R.id.channel_grid_icon_iv);
            tvtitle = (TextView) itemView.findViewById(R.id.channel_grid_title_tv);

        }
    }

    public interface mItemClickListener
    {
        void onItemClick(int position);
    }
}
